package org.bigdata.framework.admin.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.bigdata.framework.core.model.Feedback;
import org.bigdata.framework.core.protocol.IBaseService;
import org.bigdata.framework.core.protocol.IPublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FeedbackController extends BaseContoroller {

	@Autowired
	private IBaseService baseService;
	@Autowired
	private IPublicService publicService;

	//意见反馈列表界面
	@RequestMapping("user-feedback-list.html")
	public String toFeedback() {

		return "/modules/repository/feedback_list";
	}

	//意见反馈查询
	@RequestMapping("feedbackQueryMent")
	public void toFeedbackQueryMent(HttpServletRequest request, HttpServletResponse response) throws Exception{

		Integer pageSize = Integer.parseInt(request.getParameter("page"));
		String queryParam = request.getParameter("queryParams");
		String text = request.getParameter("text");

		HashMap<String, Object> queryParams = new HashMap<String, Object>();
		if (text != null && !"".equals(text)) {
			queryParams.put("%," + queryParam, text);
		}
		queryParams.put("^," + "create_time", "desc");
		Integer count = publicService.getFeedbackCount(queryParams);
		List<HashMap<String, Object>> r = new ArrayList<HashMap<String, Object>>();
		LinkedHashMap<String, Object> mp = null;
		List<Feedback> list = publicService.getFeedback(queryParams, pageSize, 15);
		for (int i = 0; i < list.size(); i++) {
			Feedback aFeedback = list.get(i);
			mp = new LinkedHashMap<String, Object>();
			mp.put("id", aFeedback.getId());
			mp.put("title", aFeedback.getTitle());
			mp.put("problem", aFeedback.getProblem());
			mp.put("name", aFeedback.getName());
			mp.put("email", aFeedback.getEmail());
			mp.put("phone", aFeedback.getPhone());
			mp.put("picture", aFeedback.getPicture());
			mp.put("createTime", aFeedback.getCreateTime());
			r.add(mp);
		}
		HashMap<String, Object> dataMp = new HashMap<String, Object>();
		dataMp.put("pageSize", (int) Math.ceil(Double.valueOf(count) / 15));
		dataMp.put("result", r);
		String result = formatToJson(dataMp);
		super.pringWriterToPage(result.toString(), "application/json", response);
		
	}
	
	//意见反馈删除
	@RequestMapping("feedbackDeleteMent")
	public void toFeedbackDeleteMent(HttpServletRequest request, HttpServletResponse response) throws Exception {

		int id = Integer.parseInt(request.getParameter("id"));
		Feedback feedback = publicService.findFeedback(id);
		try {
			baseService.deleteFeedback(feedback);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String result = formatToJson(1);
		super.pringWriterToPage(result.toString(), "application/json", response);
	}

}
